package me.zhangjin.lucene.demo.news.dao;

import me.zhangjin.lucene.demo.news.utils.EsUtils;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by bee on 17/8/11.
 */
public class Dao {

    private Connection conn;

    public void getConnection() {
        try {

            Class.forName("com.mysql.cj.jdbc.Driver");
            String user = "boot";
            String passwd = "boot";
            String url = "jdbc:mysql://zhangjin.ml:23306/boot?characterEncoding=utf8&useSSL=false";

            conn = DriverManager.getConnection(url, user, passwd);

            if (conn != null) {
                System.out.println("mysql连接成功!");
            } else {
                System.out.println("mysql连接失败!");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void mysqlToEs() {
        String sql = "SELECT * FROM news";
        TransportClient client = EsUtils.getSingleClient();

        try {

            System.out.println("开始读取 MySQL 数据");
            PreparedStatement pstm = conn.prepareStatement(sql);
            ResultSet resultSet = pstm.executeQuery();
            System.out.println("读取 MySQL 数据成功");


            System.out.println("开始写入 ES 数据");

            while (resultSet.next()) {

                Map<String, Object> map = new HashMap<>();

                int nid = resultSet.getInt(1);
                map.put("id", nid);

                map.put("title", resultSet.getString(2));
                map.put("key_word", resultSet.getString(3));
                map.put("content", resultSet.getString(4));
                map.put("url", resultSet.getString(5));
                map.put("reply", resultSet.getInt(6));
                map.put("source", resultSet.getString(7));

                String postdatetime = resultSet.getTimestamp(8).toString();
                map.put("postdate", postdatetime.substring(0, postdatetime.length() - 2));

                // {postdate=2019-09-16 15:33:00, key_word=亚泰,富力,中超, id=4552, source=sina, title=进球GIF-扎哈维手术刀传球雷纳迪尼奥推射破门, reply=1, content=　　新浪体育讯　　北京时间9月16日晚19：35，中超联赛第25轮全面打响，长春亚泰主场迎战广州富力，比赛第40分钟，扎哈维送出绝妙直塞球，雷纳迪尼奥轻巧推射远角破门，富力1-1扳平比分。, url=http://sports.sina.com.cn/china/j/2016-09-16/doc-ifxvyqwa3294610.shtml}
                System.out.println(map);

                IndexResponse response = client.prepareIndex("spnews", "news", String.valueOf(nid))
                        .setSource(map)
                        .execute()
                        .actionGet();

                // IndexResponse[index=spnews,type=news,id=4552,version=1,result=created,shards={"total":1,"successful":1,"failed":0}]
                System.out.println(response);
            }

            System.out.println("写入 ES 数据完成");

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
